package org.csu.mypetstore.web.servlets;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Log;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;

//退出登录，并设置session的account为null
public class SignOutServlet extends HttpServlet {
    private static final String SIGN_OUT = "/WEB-INF/jsp/catalog/Main.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("account");
        //时间
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = tempDate.format(new java.util.Date());
        //信息
        String info = "用户"+account.getUsername()+"退出登录";
        Log log = new Log(account.getUsername(),datetime,info);
        //添加log
        new AccountService().addLog(log);

        session.setAttribute("account", null);
        session.setAttribute("cart",null);


        request.getRequestDispatcher(SIGN_OUT).forward(request,response);
    }
}